import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 直接登录数字化平台，然后跳转到数智豫电（平台自动化刷数据使用)
 *
 * @author admin
 */
public class Brush_Mh_Szh_Szhn_2 {
    public static void main(String[] args) {

        //指定配置
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");

        //隐藏浏览器
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--start-maximized");

        //声明浏览器
        ChromeDriver driver = new ChromeDriver(chromeOptions);

        //门户登录
        driver.get("http://nsystem.ha.sgcc.com.cn/");

        driver.findElement(By.id("username")).sendKeys("leijiankui");
        driver.findElement(By.id("password")).sendKeys("0000aaaa,");
        driver.findElement(By.id("submit_login")).click();

        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        driver.getPageSource();

        String windowHandleMh = driver.getWindowHandle();
        System.out.println("门户：" + windowHandleMh);
        WebElement element = null;
        if (Brush_Mh_Szh_Szhn_2.hasElement(driver, By.cssSelector("li:nth-child(9) > span img"))) {
            element = driver.findElement(By.cssSelector("li:nth-child(9) > span img"));


            //数智豫电图标
//        driver.findElement(By.cssSelector("li:nth-child(9) > span img")).click();
//        driver.findElement(By.xpath("//img[@title='数智豫电']")).click();
//        driver.findElement(By.xpath("//li[9]/span/a/img']")).click();
//        driver.findElement(By.xpath("//li[9]/span/a/img']")).click();

//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

//        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//        driver.getPageSource();

            if (Objects.isNull(driver)) {
                return;
            }

            String windowHandleSzh = driver.getWindowHandle();
            System.out.println("数字：" + windowHandleSzh);

            Set<String> windowHandleSzhList = driver.getWindowHandles();
            for (String handle : windowHandleSzhList) {
                if (driver.getWindowHandle().equals(handle)) {
                    System.out.println("跳过：" + handle);
                    continue;
                }
                driver.switchTo().window(handle);
            }

            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            driver.getPageSource();

            //各省
            driver.findElement(By.linkText("各省")).click();
//         xdriver.findElement(By.cssSelector(".offChange > a")).click();
//        driver.findElement(By.xpath("//a[contains(text(),'各省')]")).click();
//        driver.findElement(By.xpath("//a[contains(.,'各省')]")).click();


//        driver.findElement(By.xpath("//div[@id='ces']/section/section/div/div/header/div/span/a/i")).click();
//        driver.findElement(By.cssSelector("css=a > i")).click();

            //河南
            driver.findElement(By.cssSelector(".content-box:nth-child(14) > i")).click();
//        driver.findElement(By.xpath("//div[@id='ces']/section/section/div/div/header/div/span/div/div[14]/i")).click();

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            driver.quit();  // 关闭浏览器
        } else {
            System.out.println("关闭！！！");
            Brush_Mh_Szh_Szhn_2.closeBrowser(driver);
//            driver.close();  // 关闭浏览器

        }

    }

    public static boolean hasElement(WebDriver webDriver, By by) {
        try {
            webDriver.findElement(by);
            return true;
        } catch (Exception e) {
            System.out.println("不存在元素");
            return false;
        }
    }

    public static void closeBrowser(WebDriver webDriver) {
        if (webDriver == null) {
            return;
        }
        webDriver.quit();
        webDriver = null;
    }
}
